<?php

/// +----------------------------------------------------------------------
// | 企业模块
// +----------------------------------------------------------------------
// | Author: lcx <916450508@qq.com >
// +----------------------------------------------------------------------
// | Date: 2018/12/5
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminController;
use think\facade\Cache;
use tool\Curl;
use app\common\model\CompanyModel;
use app\common\model\ApplicationsModel;

class Company extends AdminController
{

    protected $curl;
    protected $companyModel;
    protected $applicationsModel;

    /**
     * 初始化
     * node constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->curl = new Curl();
        $this->companyModel = new CompanyModel();  // 企业模型
        $this->applicationsModel = new ApplicationsModel();
    }

    /**
     * 企业列表
     */
    public function index()
    {
        $basic_data = [
            'title' => Cache::get('SysInfo')['ManageName'],
            'app_sn'=>input('param.app_sn')
        ];
        return $this->fetch('', $basic_data);
    }

    /**
     * 新增企业
     * @return mixed
     */
    public function add()
    {
        Cache::remember('Home', function () {
            return model('menu')->getHome();
        });
        $applications = $this->applicationsModel->getAll(['is_deleted' => 0, 'status' => 2], 'id,app_sn,name');//应用

        $basic_data = [
            'title' => Cache::get('SysInfo')['ManageName'],
            'nav' => model('menu')->getNav(),
            'home' => Cache::get('Home'),
            'data' => '',
            'applications' => $applications,
        ];
        return $this->fetch('', $basic_data);
    }

    // 编辑企业
    public function edit()
    {
        $id = $this->request->get('company_id');
        if (empty($id)) return msg_error('暂无数据，请重新刷新页面！');

        // 查询企业跟配置信息
        $where['a.company_id'] = $id;
        $join = [
            ['company_config c', 'c.company_id = a.company_id', 'left']  // 企业配置表--企业表
        ];
        $field = '*';
        $data = $this->companyModel->getOne($where, $field, $join);
        if (empty($data)) {
            return msg_error('暂无数据，请重新刷新页面！');
        }

        $data['wx_config'] = empty($data['wx_config']) ? '' : json_decode($data['wx_config'], true);
        $data['spread_wx_config'] = empty($data['spread_wx_config']) ? '' : json_decode($data['spread_wx_config'], true);
        $data['engineer_wx_config'] = empty($data['engineer_wx_config']) ? '' : json_decode($data['engineer_wx_config'], true);
        $data['mp_config'] = empty($data['mp_config']) ? '' : json_decode($data['mp_config'], true);
        $data['img_config'] = empty($data['img_config']) ? '' : json_decode($data['img_config'], true);
        $data['esign_config'] = empty($data['esign_config']) ? '' : json_decode($data['esign_config'], true);
        $data['sms_config'] = empty($data['sms_config']) ? '' : json_decode($data['sms_config'], true);
        $data['pay_config'] = empty($data['pay_config']) ? '' : json_decode($data['pay_config'], true);
        $applications = $this->applicationsModel->getAll([], 'id,app_sn,name');//应用
        $basic_data = [
            'title' => Cache::get('SysInfo')['ManageName'],
            'nav' => model('menu')->getNav(),
            'home' => Cache::get('Home'),
            'data' => $data,
            'applications' => $applications
        ];
        return $this->fetch('add', $basic_data);
    }

    /**
     * 申请列表
     */
    public function apply_index()
    {
        return $this->fetch();
    }

    /*
     *设置服务费
     **/
    public function sets()
    {
        return $this->fetch();
    }


}
